<script setup lang="ts">
import type { FormInstance, FormRules } from 'element-plus'
import { onMounted, reactive, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { getCodeImgApi } from '@/api/login'
import modal from '@/plugins/modal'
import useUserStore from '@/store/modules/user'

interface RuleForm {
  username: string
  password: string
  code: string
}

const loginForm = ref({
  username: 'admin',
  password: '123456',
  code: '',
  uuid: '',
})

const config = ref({
  rememberMe: false,
  isAllowRegister: true,
})

const codeConfig = ref({
  captchaEnabled: true,
  img: '',
  uuid: '',
})

const loginFormRef = ref<FormInstance>(null)
const loginFormRule = reactive<FormRules<RuleForm>>({
  username: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { min: 3, max: 18, message: '长度在 3 到 18 个字符', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 3, max: 18, message: '长度在 3 到 18 个字符', trigger: 'blur' },
  ],
  code: [
    { required: true, message: '请输入验证码', trigger: 'blur' },
    { min: 1, max: 5, message: '长度在 1 到 5 个字符', trigger: 'blur' },
  ],
})
const loading = ref(false)

async function getCodeImage() {
  const { data } = await getCodeImgApi()
  codeConfig.value = data
  loginForm.value.uuid = data.uuid
}

const redirect = ref(undefined)
const route = useRoute()
watch(
  route,
  (newRoute) => {
    redirect.value = newRoute.query && newRoute.query.redirect
  },
  { immediate: true },
)

const userStore = useUserStore()
const router = useRouter()
async function login() {
  loading.value = true
  try {
    await userStore.login(loginForm.value)
  }
  finally {
    loading.value = false
  }
  modal.msgSuccess('登录成功')
  router.push({ path: redirect.value || '/' })
}

onMounted(() => {
  getCodeImage()
})
</script>

<template>
  <div class="container" @keydown.enter="login">
    <div class="left">
      <div class="bg" />
    </div>

    <div class="main">
      <div class="form-area">
        <div class="text">
          <p class="title">
            欢迎登录系统
          </p>
          <p class="sub-title">
            Welcome
          </p>
        </div>

        <el-form
          ref="loginFormRef" :model="loginForm" :rules="loginFormRule" label-width="auto" class="login-form"
          status-icon
        >
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username" class="input" prefix-icon="user" placeholder="请输入登录账号"
              autocomplete="off"
            />
          </el-form-item>

          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password" class="input" prefix-icon="lock" placeholder="请输入登录密码"
              type="password" autocomplete="off"
            />
          </el-form-item>

          <el-form-item prop="code" class="code-box">
            <el-input
              v-model.trim="loginForm.code" maxlength="5" auto-complete="off" placeholder="验证码" class="input"
              style="flex: 1"
            >
              <template #prefix>
                <svg-icon icon-class="validCode" class="input-icon" />
              </template>
            </el-input>
            <div class="login-code" @click="getCodeImage" v-html="codeConfig.img" />
          </el-form-item>

          <div class="login-tips">
            <el-checkbox v-model="config.rememberMe">
              记住密码
            </el-checkbox>
            <el-link
              v-if="config.isAllowRegister" class="login-tips-link" type="primary" href="/register"
              target="_blank"
            >
              去注册账号
            </el-link>
          </div>

          <el-form-item>
            <el-button :loading="loading" class="btn" type="primary" @click="login">
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.container {
  display: flex;
  height: 100vh;
  flex-direction: row;

  .left {
    flex: 1;
    background: linear-gradient(164.86deg, #e7edff 0%, #cad8ff 98.62%);
    display: flex;
    justify-content: center;
    align-items: center;

    .bg {
      width: 80%;
      max-width: 600px;
      height: 60%;
      background: url(https://img.js.design/assets/img/663f1baf23d2786e7a727e72.png#96e423584f5ab564e5a3402e3a1dae3d)
        no-repeat center / contain;
    }
  }

  .main {
    flex: 1;
    padding: 40px 20px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;

    .form-area {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      width: auto;
      padding: 50px 30px;
      height: auto;
      opacity: 0.8;
      border-radius: 20px;
      background: rgba(255, 255, 255, 1);
      box-shadow: 0px 10px 40px rgba(158, 195, 255, 0.51);

      .text {
        font-family: '华文行楷', serif;
        text-align: center;
        margin-bottom: 30px;

        .title {
          font-size: 32px;
          font-weight: bold;
          margin: 5px 0;
        }

        .sub-title {
          font-size: 18px;
          color: rgba(0, 0, 0, 0.6);
        }
      }

      .login-form {
        width: 100%;
        max-width: 320px;

        .input {
          width: 100%;
          height: 40px;
          border-radius: 20px;
          background: #fff;
          border: 1px solid #d9d9d9;
        }

        .btn {
          width: 100%;
          height: 40px;
          margin-top: 20px;
        }

        .login-tips {
          display: flex;
          justify-content: space-between;
          align-items: center;
          margin-top: 10px;
        }

        .login-code {
          height: 40px;
          margin-left: 10px;
          cursor: pointer;
        }

        .code-box {
          display: flex;
          align-items: center;
        }
      }
    }
  }

  @media (max-width: 768px) {
    flex-direction: column;

    .left {
      display: none;
    }

    .main {
      padding: 20px;

      .form-area {
        width: 350px;
        height: auto;
        padding: 20px;
      }
    }
  }
}
</style>
